package com.koron.order.linedefect.base.controller;

import com.koron.bean.base.Response;
import com.koron.bean.workflow.RedirectQuery;
import com.koron.bean.workflow.WorkflowController;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.order.defect.bean.po.DefectStatus;
import com.koron.order.linedefect.base.service.DefectService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName DefectReviewController
 * @description
 * @Author zhouj
 * @Date 2021/9/24 11:42
 */
@Slf4j
@RequestMapping("/lineDefect/waitReview/")
@RestController("lineDefectReviewController")
@Api(tags = "线路缺陷工单-待审核")
public class DefectReviewController implements WorkflowController {

    @Autowired
    DefectService lineDefectService;

    private String getCurrDs() {
        return ThreadLocalContext.get().getCurrDs();
    }

    /**
     * 修改业务状态
     *
     * @param redirectQuery
     */
    @Override
    public Response<Object> updateState(@RequestBody RedirectQuery redirectQuery) {
        log.info("工单审核提交:{}", redirectQuery.toString());
        if (redirectQuery.getOperation() == 1 || redirectQuery.getOperation() == 256) {
            /*//说明驳回 比较驳回编码是否为 Process-task0
            String rejectNodeCode = redirectQuery.getRejectNodeCode();
            if (StringUtils.equals("Process-task0", rejectNodeCode)) {
                //修改工单状态为草稿
                modify(redirectQuery, DefectStatus.DRAFT.getStatus());
            }*/
            //修改工单状态为草稿
            modify(redirectQuery, DefectStatus.DRAFT.getStatus());
        } else {
            modify(redirectQuery, DefectStatus.WAIT_REVIEW.getStatus());
        }
        return Response.ok();
    }

    /**
     * 结束时的修改方法，可重写后自定义
     *
     * @param redirectQuery
     */
    @Override
    public Response<Object> updateEnd(@RequestBody RedirectQuery redirectQuery) {
        log.info("工单审核结束:{}", redirectQuery.toString());
        ADOConnection.runTask(getCurrDs(), lineDefectService, "submitDefect", Integer.class, redirectQuery.getDataId(), DefectStatus.WAIT_DISPATCH.getStatus());
        return Response.ok();
    }

    /**
     * 作废时的修改方法，可重写后自定义
     *
     * @param redirectQuery
     */
    @Override
    public Response<Object> updateCancel(@RequestBody RedirectQuery redirectQuery) {
        //作废时,只将工单状态变为已废弃即可
        log.info("工单审核作废:{}", redirectQuery.toString());
        if (redirectQuery.getOperation() == 16) {
            //说明作废
            //修改工单状态为已废弃
            modify(redirectQuery, DefectStatus.DEPRECATED.getStatus());
        }
        return Response.ok();
    }

    public void modify(RedirectQuery redirectQuery, String status) {
        //修改工单状态为草稿
        ADOConnection.runTask(getCurrDs(), lineDefectService, "updateStatus", Integer.class, redirectQuery.getDataId(), status);
    }

}
